using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Lifetime;

namespace MindFactorial.LaunchPoint.Common
{

    /// <summary>
    /// Encapsulates a remote object that can have it's life time properties set by any child classes.
    /// Inherit from this class if you want the ability to manage the life time.
    /// </summary>
    public class IPCObject : MarshalByRefObject
    {
        private int _initialLeaseTime = 5;
        private int _sponsorshipTimeout = 5;
        private int _renewOnCallTime = 5;
        private bool _isimmortal = false;

        /// <summary>
        /// creates a new IPCObject
        /// </summary>
        public IPCObject()
        {

        }        

        /// <summary>
        /// Initializes life time service object to control the lifetime policy for this instance 
        /// </summary>
        /// <returns>Returns life time service object</returns>
        public override Object InitializeLifetimeService()
        {
            if (!_isimmortal)
            {
                ILease lease = (ILease)base.InitializeLifetimeService();
                if (lease.CurrentState == LeaseState.Initial)
                {
                    lease.InitialLeaseTime = TimeSpan.FromMinutes(_initialLeaseTime);
                    lease.SponsorshipTimeout = TimeSpan.FromMinutes(_sponsorshipTimeout);
                    lease.RenewOnCallTime = TimeSpan.FromMinutes(_renewOnCallTime);
                }
                return lease;
            }
            else
                return null;
        }

        #region Properties

        /// <summary>
        /// Gets or Sets the Initial Lease time for this instance
        /// </summary>
        protected int InitialLeaseTime
        {
            get { return _initialLeaseTime; }
            set { _initialLeaseTime = value; }
        }

        /// <summary>
        /// Gets or Sets the Sponsorship Timeout for this instance
        /// </summary>
        protected int SponsorshipTimeout
        {
            get { return _sponsorshipTimeout; }
            set { _sponsorshipTimeout = value; }
        }

        /// <summary>
        /// Gets or Sets Renew OnCallTime for this instance
        /// </summary>
        protected int RenewOnCallTime
        {
            get { return _renewOnCallTime; }
            set { _renewOnCallTime = value; }
        }

        /// <summary>
        /// Gets or Sets the a value indicating whether or not the instance of this
        /// Object remains in memory for the life of the application
        /// </summary>
        protected bool IsImmortal
        {
            get { return _isimmortal; }
            set { _isimmortal = value; }
        }
        #endregion


    }


    

}
